package com.example.yiwei.crm.view.frament.contactDetailedMenu;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.view.activity.contactActivity.QRCMessageActivity;
import com.example.yiwei.crm.zxing.decoding.CaptureActivity;
import com.example.yiwei.crm.zxing.encoding.EncodingHandler;
import com.google.zxing.WriterException;

import static android.app.Activity.RESULT_OK;

/**
 * Created by Administrator on 2016/12/19.
 */

public class contactQRCFragment extends Fragment{
    @Nullable

    ContactMessageEntity contactMessageEntity;
    ImageView img_erweima;
    Button btn_saomiao;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootview=inflater.inflate(R.layout.fragment_contact_erweima,container,false);
        contactMessageEntity= GetContact.get();
        img_erweima= (ImageView) rootview.findViewById(R.id.fragment_contact_QR_img_erweima);
        btn_saomiao= (Button) rootview.findViewById(R.id.fragment_contact_QRC_btn_saomiao);
        btn_saomiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开扫描界面扫描条形码或二维码
                Intent openCameraIntent = new Intent(getActivity(),CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
            }
        });
        try {
            String contentString=contactMessageEntity.getContact_mobile_phone()+
                    "/"+contactMessageEntity.getContact_address()+"/"
                    +contactMessageEntity.getContact_respectful()
                    +"/"+contactMessageEntity.getContact_name();
            if (!contentString.equals("")) {
                //根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（350*350）
                Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);
                img_erweima.setImageBitmap(qrCodeBitmap);
            }else {
                Toast.makeText(getActivity(), "Text can not be empty", Toast.LENGTH_SHORT).show();
            }

        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rootview;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //处理扫描结果（在界面上显示）
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            Intent intent=new Intent(getActivity(),QRCMessageActivity.class);
            intent.putExtra("erweima",scanResult);
            startActivity(intent);

        }
    }

}
